package com.example.weibo_suhuanzhen.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.example.weibo_suhuanzhen.R;
import com.example.weibo_suhuanzhen.UserSession;
import com.example.weibo_suhuanzhen.sharedPreferences.SharedPreferencesManager;

public class StartActivity extends Activity {
    // 声明字符常量
    private static final String PRIVACY_POLICY_AGREED = "privacy_policy_agreed";
    private boolean hasAgreed;
    private SharedPreferencesManager setting;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_view);
        checkPrivacyPolicy();

    }
    private void checkPrivacyPolicy() {
        setting=SharedPreferencesManager.getInstance(this);
        hasAgreed =setting.getBoolean(PRIVACY_POLICY_AGREED,false);

        if (!hasAgreed) {
            showPrivacyPolicyDialog();
        }
        else         // 延迟0.5秒启动下一个Activity
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    proceedToNextActivity();
                }
            }, 1000); // 1000毫秒的延迟
    }
    private void showPrivacyPolicyDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.CustomAlertDialog);
        LayoutInflater inflater = getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialog_privacy_policy, null);
        builder.setView(dialogView);

        AlertDialog alertDialog = builder.create();

        TextView privacyPolicyText = dialogView.findViewById(R.id.privacy_policy_text);
        TextView buttonAgree = dialogView.findViewById(R.id.buttonAgree);
        TextView buttonDisagree = dialogView.findViewById(R.id.buttonDisagree);

        String text = "欢迎使用 iH微博 ，我们将严格遵守相关法律和隐私政策保护您的个人隐私，请您阅读并同意《用户协议》与《隐私政策》";
        SpannableString spannableString = new SpannableString(text);

        // 设置《用户协议》可点击
        int userAgreementStart = text.indexOf("《用户协议》");
        int userAgreementEnd = userAgreementStart + "《用户协议》".length();
        ClickableSpan userAgreementClick = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                // 处理用户协议点击事件
                Toast.makeText(StartActivity.this, "用户协议点击", Toast.LENGTH_SHORT).show();
                // 这里可以启动一个新的 Activity 或显示一个 WebView
            }
        };
        spannableString.setSpan(userAgreementClick, userAgreementStart, userAgreementEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        // 设置《隐私政策》可点击
        int privacyPolicyStart = text.indexOf("《隐私政策》");
        int privacyPolicyEnd = privacyPolicyStart + "《隐私政策》".length();
        ClickableSpan privacyPolicyClick = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                // 处理隐私政策点击事件
                Toast.makeText(StartActivity.this, "隐私政策点击", Toast.LENGTH_SHORT).show();
            }
        };
        spannableString.setSpan(privacyPolicyClick, privacyPolicyStart, privacyPolicyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        privacyPolicyText.setText(spannableString);
        privacyPolicyText.setMovementMethod(LinkMovementMethod.getInstance());

        buttonAgree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setting.saveBoolean(PRIVACY_POLICY_AGREED, true);
                alertDialog.dismiss();
                // 延迟1秒启动下一个Activity
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        proceedToNextActivity();
                    }
                }, 1000);
            }
        });

        buttonDisagree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                finish(); // 关闭应用程序
            }
        });

        alertDialog.show();
    }

    private void proceedToNextActivity() {

        if (setting.getBoolean("isLoggedIn", false))
            UserSession.getInstance().setLoggedIn(true);
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

}
